import { DeliverLogModel } from '@/models/deliverLog'
import { DeliverLogUpdateDTO, saveDeliverLog, updateDeliverLog } from '@/services/DeliverLogService'
import { ActionType, PageContainer } from '@ant-design/pro-components'
import { useSnapshot } from '@umijs/max'
import { message } from 'antd/lib'
import { useRef, useState } from 'react'
import DeliverLogTable from './components/DeliverLogTable'
import DeliverLogUpdateModal from './components/DeliverLogUpdateModal'

const DeliverLog = () => {
  // 表格引用
  const tableRef = useRef<ActionType>()
  let deliverLogState = useSnapshot(DeliverLogModel.state)
  let [isAdd, setIsAdd] = useState(false)

  // 提交修改
  const onUpdateFinish = async (formData: any) => {
    let dto: DeliverLogUpdateDTO = JSON.parse(JSON.stringify(formData))
    dto.fee = dto.fee * 100
    if (isAdd) {
      saveDeliverLog(dto)
      message.success('操作成功')
      tableRef.current?.reload()
    } else {
      updateDeliverLog(dto)
      message.success('操作成功')
      tableRef.current?.reload()
    }
    return true

    // let func = deliverLogState.deliverLog ? updateDeliverLog : saveDeliverLog
    // if (await func(dto)) {
    //     message.success('操作成功')
    //     // 刷新表格
    //     tableRef.current ?. reload()
    //     return true
    // }
    // return false
  }
  return (
    <PageContainer>
      {JSON.stringify(isAdd)}
      <DeliverLogTable tableRef={tableRef} setIsAdd={setIsAdd} />
      <DeliverLogUpdateModal isAdd={isAdd} onFinish={onUpdateFinish} />
    </PageContainer>
  )
}
export default DeliverLog
